#!/usr/bin/env python3
"""
node_exporter MCP Server

面向软件：golang-github-prometheus-node_exporter (node_exporter)

所有 MCP tool 列表：
- node_exporter_service_test_execution: 测试 node_exporter 服务的执行状态
- node_exporter_service_test_reload: 测试 node_exporter 服务的重载功能

参数说明：
- service_name: 服务名称字符串

返回 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("Node Exporter MCP Server")

@mcp.tool()
def node_exporter_service_test_execution(service_name: str = "node_exporter.service") -> dict:
    """
    测试 node_exporter 服务的执行状态
    
    参数:
        service_name: 服务名称，默认为 node_exporter.service
        
    返回:
        JSON 结构包含命令执行结果
    """
    try:
        cmd = ["systemctl", "status", service_name]
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
        
        return {
            "success": result.returncode == 0,
            "command": " ".join(cmd),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "systemctl status " + service_name,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def node_exporter_service_test_reload(service_name: str = "node_exporter.service") -> dict:
    """
    测试 node_exporter 服务的重载功能
    
    参数:
        service_name: 服务名称，默认为 node_exporter.service
        
    返回:
        JSON 结构包含命令执行结果
    """
    try:
        cmd = ["systemctl", "reload", service_name]
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
        
        return {
            "success": result.returncode == 0,
            "command": " ".join(cmd),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "systemctl reload " + service_name,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()